#pragma once

#include<unistd.h>
#include<iostream>
#include<cassert>
#include<cstring>
#include<cstdio>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/stat.h>
#include<sys/types.h>
#include"Log.hpp"
#include<fcntl.h>
using namespace std;

#define PATH_NAME "."
#define PROJ_ID 0x66
//操作系统管理物理内存时，页的大小是4k，也就是4096，如果申请了4097，就一个字节，OS底层会开辟4096*2的空间，多余空间浪费且访问不了
#define SHM_SIZE 4096 //共享内存的大小，最好是页(PAGE:4096)的整数倍

#define FIFO_NAME "./fifo"

class Init
{
public:
    Init()
    {
        umask(0);
        int n = mkfifo(FIFO_NAME,0666);
        assert(n == 0);
        (void)n;
        Log("creat fifo success",Notice) << "\n";
    }
    ~Init()
    {
        unlink(FIFO_NAME);
        Log("remove fifo success",Notice) << "\n";
    }
};

#define READ O_RDONLY 
#define WRITE O_WRONLY

int OpenFIFO(std::string pathname,int flags)
{
    int fd = open(pathname.c_str(),flags);
    assert(fd >= 0);
    return fd;
}

void Wait(int fd)
{
    Log("等待中...",Notice) << "\n";
    uint32_t temp = 0;
    ssize_t s = (fd,&temp,sizeof(uint32_t));
    assert(s == sizeof(uint32_t));
    (void)s;
}

void Signal(int fd)
{
    uint32_t temp = 1;
    ssize_t s = write(fd,&temp,sizeof(uint32_t));
    assert(s == sizeof(uint32_t));
    (void)s;
    Log("唤醒中...",Notice) << "\n";
}

void CloseFifo(int fd)
{
    close(fd);
}

